from pymongo import MongoClient


def main():
    with open('./username.csv', 'r') as f:
        username_list = f.read().split(',')

    with MongoClient('mongodb://localhost:27017') as conn:
        db = conn['cloud']
        user_id_map = {item.get('_id'): item.get('username') for item in db['user'].find({'username': {'$in': username_list}})}

        with open('./trade_log.csv', 'w', encoding='utf-8') as f:
            for item in db['trade_log'].find({'user_id': {'$in': list(user_id_map.keys())}}):
                item['username'] = user_id_map.get(item['user_id'])
                f.writelines(str(item))
                f.writelines('\n')


if __name__ == '__main__':
    main()

